let date = new Date()
// 当前年
let year = date.getFullYear()+''
// 当前月
let month = (date.getMonth() + 1).toString().padStart(2, '0')
// 当前日
let day = date.getDate().toString().padStart(2, '0')
// 当前周围
let week = date.getDay()==0?7:date.getDay()

// 本周日期 [{yaer:"2022",month:"11","dates":"05"}]
let weekArray = getWeekDay(`${year}-${month}-${day}`).map(item => {
  let arr = item.split('-')
  let year = arr[0]
  let month = arr[1].toString().padStart(2, '0')
  let day = arr[2].toString().padStart(2, '0')
  return {
    year,
    month,
    day
  }
})

// 当前月日期
function getMonthDate(){
  let monthLength = new Date(year,month,0).getDate()
  return Array(monthLength).fill(0).map((v,i)=>{
    return (i+1+'').padStart(2,'0')
  })
}
function getWeekDay(dateString) {
  // console.log(dateString);
  let dateStringReg = /^\d{4}[/-]\d{1,2}[/-]\d{1,2}$/;
  if (dateString.match(dateStringReg)) {
    let presentDate = new Date(dateString),
      today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7;
    return Array.from(new Array(7), function(val, index) {
      return formatDate(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 * 1000));
    });
  } else {
    throw new Error('dateString should be like "yyyy-mm-dd" or "yyyy/mm/dd"');
  }

  function formatDate(date) {
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  }
}

// 上一年
let lastYear = new Date(year - 1)+''
// 上一个月
let lastMonth = new Date(year, month - 2).getMonth()+''

// 上一个周
// console.log(`${year}-${month}-${day-week}`);

let lastWeekDate = getLastWeekDate(year,month,day,week)
function getLastWeekDate(year,month,day,week){
  let date = new Date(`${year}-${month}-${day}`)
  return new Date(date.getTime()-(week*24*60*60*1000))
}
console.log(lastWeekDate);
let lastWeekYear = lastWeekDate.getFullYear()
let lastWeekMonth = (lastWeekDate.getMonth()+1).toString().padStart(2, '0')
let lastWeekDay = lastWeekDate.getDate().toString().padStart(2, '0')
// 上一个周的日期
// console.log(lastWeekYear,lastWeekMonth,lastWeekDay);
console.log(`${lastWeekYear}-${lastWeekMonth}-${lastWeekDay}`);
let lastWeekArray = getWeekDay(`${lastWeekYear}-${lastWeekMonth}-${lastWeekDay}`).map(item => {
  let arr = item.split('-')
  let year = arr[0]
  let month = arr[1].toString().padStart(2, '0')
  let day = arr[2].toString().padStart(2, '0')
  return {
    year,
    month,
    day
  }
})
// 传入日期计算出星期几
function getWeek(date){
  let code = new Date(date).getDay()
  switch(code) {
    case 0:
      return '星期天'
      break
    case 1:
      return '星期一'
      break
    case 2:
      return '星期二'
      break
    case 3:
      return '星期三'
      break
    case 4:
      return '星期四'
      break
    case 5:
      return '星期五'
      break
    case 6:
      return '星期六'
  }
}

export {
  year,
  month,
  day,
  weekArray,
  lastYear,
  lastMonth,
  lastWeekArray,
  getWeek,
  getWeekDay,
  getMonthDate
}
